public with sharing class LookupPopupController {

    public List <PricebookEntry>  pricebookEntries { get; set; }

    public Boolean isDisplay { get; set; }
        
    public String productName { get; set; }
    
     // Price book name which is going to be used For adding Opportunity product
    private String PRICE_BOOK_NAME='Standard Price Book'; 
    
    public LookupPopupController (){
    
       pricebookEntries = fetchAllProducts();
          if(null!=pricebookEntries && !pricebookEntries.isEmpty()){
        isDisplay=true;
        }else{
        isDisplay=false;
        }        
       Won_and_done_process_settings__c priceBookSettings =Won_and_done_process_settings__c.getValues('Price_book_name');
       //Setting price book name if present in setting
    if(null != priceBookSettings ){
        PRICE_BOOK_NAME= (null!=priceBookSettings.get('Value__c')?String.valueOf(priceBookSettings.get('Value__c')):PRICE_BOOK_NAME);
    }
    }

    public PageReference searchProducts() {
    System.debug('## Product name: ='+productName );
    
    String searchStr= '%'+productName +'%';
        if(productName !=null){
            pricebookEntries =[Select p.Id,p.name From PriceBookEntry p where p.Pricebook2.Name=:PRICE_BOOK_NAME and Name like :searchStr];
         }
        else{
            pricebookEntries =fetchAllProducts();
            
        }
        if(null!=pricebookEntries && !pricebookEntries.isEmpty()){
        isDisplay=true;
        }else{
        isDisplay=false;
        }        
        return null;
    }
    
    public  List<PricebookEntry> fetchAllProducts(){
     return [Select  p.ProductCode, p.Product2Id, p.Pricebook2Id, p.Name, p.Id From PricebookEntry p  where p.Pricebook2.Name=:PRICE_BOOK_NAME];

    }

}